//
//  UINavigationBar+Appearance.swift
//  EasyTrademarkRegqurySearch
//
//  Created by 伊伊 on 2024/2/29.
//

import Foundation
import UIKit


 extension UINavigationBar{
   
     @objc  func  setNabarClearBackgroundImage(){
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor : UIColor.clear]
        appearance.titleTextAttributes = R.attributes.nabarTitleText
        appearance.setBackIndicatorImage(R.image.backIndicatorImage, transitionMaskImage: R.image.backIndicatorImage)
        self.standardAppearance = appearance
        self.scrollEdgeAppearance = appearance
    }
    
     @objc func setNabarBackIndicatorImage(_ backIndicatorImage: UIImage,_ withTintColor: UIColor? = nil){
        let appearance = self.standardAppearance
        appearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor : UIColor.clear]
        let originalImage:UIImage = withTintColor != nil ? backIndicatorImage.withTintColor(withTintColor!, renderingMode: .alwaysOriginal) : backIndicatorImage.withRenderingMode(.alwaysOriginal)
        appearance.setBackIndicatorImage(originalImage, transitionMaskImage:originalImage)
        self.standardAppearance = appearance
        self.scrollEdgeAppearance = appearance
    }
    
     @objc func setNabarTitleTextAttributes(_ titleTextAttributes: [NSAttributedString.Key:Any]){
        let nabarAppreance = self.standardAppearance
         nabarAppreance.titleTextAttributes = titleTextAttributes
         self.standardAppearance = nabarAppreance
         self.scrollEdgeAppearance = nabarAppreance
    }
    
     @objc func setNabarScrollEdgeClearAppearance(scrollEdgeTitleTextAttributes:[NSAttributedString.Key : Any],standardTitleTextAttributes:[NSAttributedString.Key : Any],scrollEdgeTintColor:UIColor,standardTintColor:UIColor, standardBackgroundColor:UIColor = .white){
        let scrollEdgeAppearance = UINavigationBarAppearance()
        scrollEdgeAppearance.configureWithTransparentBackground()
        scrollEdgeAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor : UIColor.clear]
        
        scrollEdgeAppearance.titleTextAttributes = scrollEdgeTitleTextAttributes
 
        
        let image1 = R.image.backIndicatorImage?.withTintColor(scrollEdgeTintColor).withRenderingMode(.alwaysOriginal)
        
        scrollEdgeAppearance.setBackIndicatorImage(image1, transitionMaskImage:image1)
        
        let standardAppearance = UINavigationBarAppearance()
   

        let image2 = R.image.backIndicatorImage?.withTintColor(standardTintColor).withRenderingMode(.alwaysOriginal)
        
        standardAppearance.setBackIndicatorImage(image2, transitionMaskImage:image2)
        
        
        //设置背景相关backButton
        standardAppearance.backButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.clear]

        //设置背景相关
        standardAppearance.titleTextAttributes = standardTitleTextAttributes
        standardAppearance.backgroundColor = standardBackgroundColor
        standardAppearance.backgroundImage = nil
        standardAppearance.backgroundEffect = nil
        standardAppearance.shadowColor = nil
        standardAppearance.shadowImage = nil
        standardAppearance.titleTextAttributes = standardTitleTextAttributes
        
        self.standardAppearance = standardAppearance
        self.scrollEdgeAppearance = scrollEdgeAppearance
    }
    
    
    
     @objc func setNabarDefaultAppearance(){
        let nabarAppreance = UINavigationBarAppearance()
        //设置背景相关backButton
        nabarAppreance.backButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.clear]
        nabarAppreance.setBackIndicatorImage(R.image.backIndicatorImage?.withRenderingMode(.alwaysOriginal), transitionMaskImage: R.image.backIndicatorImage?.withRenderingMode(.alwaysOriginal))
        
        //设置背景相关
         nabarAppreance.titleTextAttributes = R.attributes.nabarTitleText
         nabarAppreance.backgroundColor = R.color.nabarColor
         nabarAppreance.backgroundImage = nil
         nabarAppreance.backgroundEffect = nil
         nabarAppreance.shadowColor = nil
         nabarAppreance.shadowImage = nil
         self.standardAppearance = nabarAppreance
         self.scrollEdgeAppearance = nabarAppreance
    }
    
}
